package wnhz.com.business;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.freeshop.FreeShopActivity;
import wnhz.com.business.groupshop.MainActivity;
import wnhz.com.business.groupshop.bean.InfoBean;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.rebateshop.RebateShopActivity;
import wnhz.com.business.shareshop.ShareShopActivity;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.Url;

/**
 * Created by lizhenglong on 2017-09-18 0018.
 * APP入口界面-免费商城,返利商城,拼团商城
 */

public class BeginActivity extends MyBaseActivity implements View.OnClickListener {
    @BindView(R.id.btn_begin_mianfei)
    ImageView btn_begin_mianfei;//免费商城

    @BindView(R.id.btn_begin_fanli)
    ImageView btn_begin_fanli;//返利商城

    @BindView(R.id.btn_begin_pintuan)
    ImageView btn_begin_pintuan;//拼团商城

    @BindView(R.id.btn_begin_gongxiang)
    ImageView btn_begin_gongxiang;//拼团商城
    @BindView(R.id.tv_tt)
    TextView tvTt;

    private long exitTime = 0;
    private Activity activity;

    @Override
    protected void handleMsg(Message msg) {

    }


    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, BeginActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin);
        ButterKnife.bind(this);
        btn_begin_mianfei.setOnClickListener(this);
        btn_begin_fanli.setOnClickListener(this);
        btn_begin_pintuan.setOnClickListener(this);
        btn_begin_gongxiang.setOnClickListener(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //透明导航栏
//            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //改变状态栏颜色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            tvTt.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvTt.getLayoutParams();
            params.height = statusHeight;
            tvTt.setLayoutParams(params);
        }
//        login();
    }

    private void login() {
        Map<String, String> map = new HashMap<>();
        SharedPreferences sp = getSharedPreferences("userinfo", 0);
        final String phone = sp.getString("username", "0");
        final String password = sp.getString("password", "0");
        if ("0".equals(phone) && "0".equals(password)) {
//            MyApplication.getInstance().BackLoginActivity();
            return;
        } else {
            map.put("mobile", Base64Util.encodedString2(phone));
            map.put("password", Base64Util.encodedString2(password));
            map.put("type", "1");
            OkGo.<InfoBean>post(Url.USER_LOGIN)
                    .tag(this)
                    .params(map, false)
                    .execute(new DialogCallback<InfoBean>(this) {
                        @Override
                        public void onSuccess(Response<InfoBean> response) {
                            if (response.body().getRe().equals("1")) {
                                Log.e("登录onSuccess   免费商城", "" + response.body().getInfo());
                                SharedPreferences sp = getSharedPreferences("userinfo", 0);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("username", phone);
                                editor.putString("password", password);
                                editor.putString("token", response.body().getToken());
                                editor.putString("logintype", "1");
                                editor.commit();
                                InfoBean infoBean = new InfoBean();
                                infoBean.setToken(response.body().getToken());
                                infoBean.setLogintype("2");
                                MyApplication.getInstance().setInfoBean(infoBean);

                            } else {
                                MyToast(response.body().getInfo());
                            }
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                        }

                        @Override
                        public void onError(Response<InfoBean> response) {
                            super.onError(response);
                        }
                    });
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.btn_begin_mianfei:
                intent = new Intent(BeginActivity.this, FreeShopActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_begin_fanli:
                intent = new Intent(BeginActivity.this, RebateShopActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_begin_pintuan:
                intent = new Intent(BeginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_begin_gongxiang:
                intent = new Intent(BeginActivity.this, ShareShopActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次后退键将退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        new Toast(getApplicationContext()).cancel();
                    }
                });
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        intent.addCategory(Intent.CATEGORY_HOME);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
                        MyApplication.getInstance().exit(activity);

                    }
                }, 200);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
